package cn.njupt.gis.gml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import cn.njupt.gis.geo.Bound;
import cn.njupt.gis.geo.Coordinate;
import cn.njupt.gis.geo.FeatureCollection;
import cn.njupt.gis.geo.FeatureMember;
import cn.njupt.gis.geo.GeoData;
import cn.njupt.gis.geo.Polygon;

public class GmlHandler extends DefaultHandler
{
	public FeatureCollection features;
	
	private Bound bound;
	private FeatureMember feature;
 
	private GeoData geodata;
	
	private StringBuffer buffer  ;
	
	private boolean bsetx = true; //bound x flag
	private boolean bsety = true; //bound y flag
	private boolean bgeoAttribute = false;
	
	public GmlHandler(FeatureCollection _features)
	{
		features = _features;
		 
		
	}
 
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		 buffer = new StringBuffer();
		 buffer.append(ch, start, length);
	}
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		 
	}
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		 
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equalsIgnoreCase("gml:featureMember"))
		{
			feature = new FeatureMember();
			features.addFeature(feature);
		}
		else if(qName.equalsIgnoreCase("gml:Polygon")) 
		{
			//这里应该是使用一个内嵌的数据库去判断数据类型，仅用一个polygon作为实验
			geodata = new Polygon();
			 
			feature.setGeoType( qName.substring(3));
			feature.putAttr(feature.getGeoType(), geodata);
			bgeoAttribute = true;
		}
		else if(qName.equalsIgnoreCase("gml:boundedBy"))
		{
			bound = new Bound();
			features.setBound(bound);
		}
 		else if(!qName.matches("gml:\\w+"))
		{
			bgeoAttribute = false;
			
		}
		
	}
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		String value = new String(buffer);
		if(qName.equalsIgnoreCase("gml:x"))
		{
			  if(bsetx)
			  {
				  bound.setMinx( Double.parseDouble(value));
				  bsetx = false;
			  }
			  else
			  {
				  bound.setMaxx(Double.parseDouble(value));
				   
			  }
		}
		if(qName.equalsIgnoreCase("gml:y"))
		{
			  if(bsety)
			  {
				  bound.setMiny( Double.parseDouble(value));
				  bsety = false;
			  }
			  else
			  {
				  bound.setMaxy( Double.parseDouble(value));
				  
			  }
		}
		if(bgeoAttribute)
		{
			if(qName.equalsIgnoreCase("gml:coordinates"))
			{
				 
				String[] coords = value.split("\\s");
				for(String strcoord:coords)
				{
					String strxy[] = strcoord.split(",");
					if(strxy.length != 2)
						continue;
					if(strxy[0].equals("")||strxy[1].equals(""))
					{
						continue;
					}
					double x = Double.parseDouble(strxy[0].trim());
					double y = Double.parseDouble(strxy[1].trim());
					Coordinate coord = new Coordinate(x,y);
				 
					geodata.addPoint(coord);
				}
				
			}
		}

		else if(!qName.matches("gml:\\w+"))
		{
			feature.putAttr(qName.split(":")[1], value);
 
		}
		 
		
	}

	@Override
	public void error(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		 
	}




	
}
